/*
 * Created by JFormDesigner on Fri Dec 14 20:43:28 CET 2007
 */

package info.rflow.preferences;

import info.rflow.Utils;
import sun.font.TrueTypeFont;

import javax.swing.*;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;


/**
 * @author Holger Brandl
 */
public class GeneralSettings extends AbstractSettingsPanel {

    private static final String WRITE_LOGFILE = "advncd.writeLogFile";
    private static final Boolean WRITE_LOGFILE_DEFAULT = false;

    public static final String NAV2BLOCK_CMD = "nav2block.cmd";
    public static final String NAV2BLOCK_CMD_DEFAULT = "/usr/local/bin/idea --line $linenumber$ $file$";

    public static final String AUTO_SAVE = "auto.save.workflows";
    public static final boolean AUTO_SAVE_DEFAULT = false;

    public static final String AUTO_LOAD = "auto.load.workflows";
    public static final boolean AUTO_LOAD_DEFAULT = false;

    public static final String SKIPPING_BEHAVIOR = "advncd.general.skipbehavior";
//    public static final int SKIPPING_BEHAVIOR_DEFAULT = LearnMethod.SKIP_UNTIL_NEXT;


    public GeneralSettings() {
        initComponents();

//        skipMap.put(sesSkipRadio.getModel(), LearnMethod.SKIP_BUT_KEEP);
//        skipMap.put(nextSesSkipRadio.getModel(), LearnMethod.SKIP_UNTIL_NEXT);
//        skipMap.put(tomorrowSkipRadio.getModel(), LearnMethod.SKIP_UNTIL_TOMORROW);
    }


    void resetPanelSettings() {
        Utils.getPrefs().remove(SKIPPING_BEHAVIOR);
        Utils.getPrefs().remove(WRITE_LOGFILE);
        Utils.getPrefs().remove(NAV2BLOCK_CMD);

        Utils.getPrefs().remove(AUTO_SAVE);
        Utils.getPrefs().remove(AUTO_LOAD);

        loadDefaults();
    }


    void applySettingsChanges() {
//        Utils.getPrefs().putBoolean(SHOW_NOT_ADDED_WARNING, usePerSetsBox.isSelected());
        Utils.getPrefs().putBoolean(WRITE_LOGFILE, writeLogFileCheckbox.isSelected());
        Utils.getPrefs().putBoolean(AUTO_SAVE, autoSaveButton.isSelected());
        Utils.getPrefs().putBoolean(AUTO_LOAD, autoLoadButton.isSelected());
        Utils.getPrefs().put(NAV2BLOCK_CMD, nav2BlockCmdField.getText());

    }


    protected void loadDefaults() {
        writeLogFileCheckbox.setSelected(Utils.getPrefs().getBoolean(WRITE_LOGFILE, WRITE_LOGFILE_DEFAULT));
        nav2BlockCmdField.setText(Utils.getPrefs().get(NAV2BLOCK_CMD, NAV2BLOCK_CMD_DEFAULT));

        autoSaveButton.setSelected(Utils.getPrefs().getBoolean(AUTO_SAVE, AUTO_SAVE_DEFAULT));
        autoLoadButton.setSelected(Utils.getPrefs().getBoolean(AUTO_LOAD, AUTO_LOAD_DEFAULT));

//        usePerSetsBox.setSelected(Utils.getPrefs().getBoolean(SHOW_NOT_ADDED_WARNING, SHOW_NOT_ADDED_WARNING_DEFAULT));

//        int defaultSkipBehavior = Utils.getPrefs().getInt(SKIPPING_BEHAVIOR, SKIPPING_BEHAVIOR_DEFAULT);
//        switch (defaultSkipBehavior) {
//            case LearnMethod.SKIP_BUT_KEEP:
//                sesSkipRadio.setSelected(true);
//                break;
//            case LearnMethod.SKIP_UNTIL_NEXT:
//                nextSesSkipRadio.setSelected(true);
//                break;
//            case LearnMethod.SKIP_UNTIL_TOMORROW:
//                tomorrowSkipRadio.setSelected(true);
//                break;
//            default:
//                throw new RuntimeException("invalid skipping behavior");
//        }
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner non-commercial license
        ResourceBundle bundle = ResourceBundle.getBundle("info.rflow.translation");
        boxPanel = new JPanel();
        autoSaveButton = new JCheckBox();
        autoLoadButton = new JCheckBox();
        learnPanel2 = new JPanel();
        label2 = new JLabel();
        nav2BlockCmdField = new JTextField();
        panel3 = new JPanel();
        writeLogFileCheckbox = new JCheckBox();

        //======== this ========
        setLayout(new GridBagLayout());
        ((GridBagLayout) getLayout()).columnWidths = new int[]{0, 0};
        ((GridBagLayout) getLayout()).rowHeights = new int[]{0, 0, 0, 0, 0};
        ((GridBagLayout) getLayout()).columnWeights = new double[]{1.0, 1.0E-4};
        ((GridBagLayout) getLayout()).rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, 1.0E-4};

        //======== boxPanel ========
        {
            boxPanel.setBorder(new CompoundBorder(
                    new TitledBorder(null, bundle.getString("GeneralSettings.boxPanel.border"), TitledBorder.LEADING, TitledBorder.TOP),
                    new EmptyBorder(5, 5, 5, 5)));
            boxPanel.setMinimumSize(null);
            boxPanel.setPreferredSize(null);
            boxPanel.setLayout(new GridBagLayout());
            ((GridBagLayout) boxPanel.getLayout()).columnWidths = new int[]{0, 27, 0};
            ((GridBagLayout) boxPanel.getLayout()).rowHeights = new int[]{0, 0, 0};
            ((GridBagLayout) boxPanel.getLayout()).columnWeights = new double[]{0.0, 0.0, 1.0E-4};
            ((GridBagLayout) boxPanel.getLayout()).rowWeights = new double[]{0.0, 0.0, 1.0E-4};

            //---- autoSaveButton ----
            autoSaveButton.setText("Save files on frame deactivation");
            autoSaveButton.setActionCommand("Save files on frame deactivation");
            boxPanel.add(autoSaveButton, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 5, 5), 0, 0));

            //---- autoLoadButton ----
            autoLoadButton.setText("Sync workflows with files on frame activation");
            boxPanel.add(autoLoadButton, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 5), 0, 0));
        }
        add(boxPanel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 5, 0), 0, 0));

        //======== learnPanel2 ========
        {
            learnPanel2.setBorder(new CompoundBorder(
                    new TitledBorder(null, bundle.getString("GeneralSettings.learnPanel2.border"), TitledBorder.LEADING, TitledBorder.TOP),
                    new EmptyBorder(5, 5, 5, 5)));
            learnPanel2.setLayout(new GridBagLayout());
            ((GridBagLayout) learnPanel2.getLayout()).columnWidths = new int[]{0, 0};
            ((GridBagLayout) learnPanel2.getLayout()).rowHeights = new int[]{0, 0, 0};
            ((GridBagLayout) learnPanel2.getLayout()).columnWeights = new double[]{1.0, 1.0E-4};
            ((GridBagLayout) learnPanel2.getLayout()).rowWeights = new double[]{0.0, 0.0, 1.0E-4};

            //---- label2 ----
            label2.setText(bundle.getString("GeneralSettings.label2.text"));
            learnPanel2.add(label2, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 5, 0), 0, 0));
            learnPanel2.add(nav2BlockCmdField, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 0), 0, 0));
        }
        add(learnPanel2, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 5, 0), 0, 0));

        //======== panel3 ========
        {
            panel3.setBorder(new CompoundBorder(
                    new TitledBorder(null, bundle.getString("GeneralSettings.panel3.border"), TitledBorder.LEADING, TitledBorder.TOP),
                    new EmptyBorder(5, 5, 5, 5)));
            panel3.setLayout(new GridBagLayout());
            ((GridBagLayout) panel3.getLayout()).columnWidths = new int[]{0, 0, 0, 0};
            ((GridBagLayout) panel3.getLayout()).rowHeights = new int[]{0, 0};
            ((GridBagLayout) panel3.getLayout()).columnWeights = new double[]{0.0, 1.0, 0.0, 1.0E-4};
            ((GridBagLayout) panel3.getLayout()).rowWeights = new double[]{0.0, 1.0E-4};

            //---- writeLogFileCheckbox ----
            writeLogFileCheckbox.setText(bundle.getString("GeneralSettings.writeLogFileCheckbox.text"));
            panel3.add(writeLogFileCheckbox, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 5), 0, 0));
        }
        add(panel3, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 5, 0), 0, 0));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
        loadDefaults();
    }


    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner non-commercial license
    private JPanel boxPanel;
    private JCheckBox autoSaveButton;
    private JCheckBox autoLoadButton;
    private JPanel learnPanel2;
    private JLabel label2;
    private JTextField nav2BlockCmdField;
    private JPanel panel3;
    private JCheckBox writeLogFileCheckbox;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
